package com.example.wangfanwangluo.wangfanboss;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wangfanwangluo.wangfanboss.Adapters.SearchAdapter;
import com.example.wangfanwangluo.wangfanboss.Models.SearchModel;

import java.util.ArrayList;
import java.util.List;

public class Search extends AppCompatActivity {
    private ListView lv;
    private SearchAdapter adapter;
    private List<SearchModel> list = new ArrayList<>();
    private TextView backbtn;
    private EditText searchet;
    private TextView searchbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ActionBar actionbar = getSupportActionBar();
        if (actionbar != null) {
            actionbar.hide();
        }
        initviews();
//        setData(); //数据

    }

    //初始化ui
    public void initviews() {
        backbtn = (TextView) findViewById(R.id.backbtn_search);
        searchet = (EditText) findViewById(R.id.searchedit_search);
        searchbtn = (TextView) findViewById(R.id.searchbtn_search);
        //创建listview的适配器.listview
        adapter = new SearchAdapter(this, list);
        lv = (ListView) findViewById(R.id.listview_search);
        lv.setAdapter(adapter);
        backbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回上页
                finish();
            }
        });

        searchbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入搜索，请求数据
                String searchstr = searchet.getText().toString();
                if (searchstr.equals("001")) {
                    Toast.makeText(Search.this, "搜索001", Toast.LENGTH_LONG).show();
                    //刷新listview
                    setData();
                    // 更新界面
                    adapter.notifyDataSetChanged();//刷新适配器
                }
            }
        });


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取listview中点击item的数据
                SearchModel item = (SearchModel) parent.getItemAtPosition(position);
//                Log.i("eee", item.getGoodsID() + " == " + item.getPosition());
                //获取当前listitem的id值
                Log.i("eee", item.getGoodsID());
                Toast.makeText(Search.this, "ID值为" + item.getGoodsID(), Toast.LENGTH_SHORT).show();
                //进入详情页面
                Intent intent = new Intent(Search.this,DocumentDetail.class);
                Bundle bundle = new Bundle();
                bundle.putString("type", "detail");
                intent.putExtras(bundle);     //将bundle传入intent中。
                startActivity(intent);

            }
        });

    }
    //点击键盘的return进行搜索
//        searchet.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                /*判断是否是“GO”键*/
//                if(actionId == EditorInfo.IME_ACTION_GO){
//                    /*隐藏软键盘*/
//                    InputMethodManager imm = (InputMethodManager) v
//                            .getContext().getSystemService(
//                                    Context.INPUT_METHOD_SERVICE);
//                    if (imm.isActive()) {
//                        imm.hideSoftInputFromWindow(
//                                v.getApplicationWindowToken(), 0);
//                    }
//
//                    searchet.setText("success");
//                    Toast.makeText(Search.this,"搜索",Toast.LENGTH_LONG).show();
//
////                    webview.loadUrl(URL);
//
//                    return true;
//                }
//                return false;
//            }
//        });

//}

    private void setData() {
        //假数据
        SearchModel firstModel = new SearchModel("001", "eijtiwi");
        list.add(firstModel);

    }



}

